package com.tao.lab

import android.os.Bundle
import android.util.Log
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayout
import androidx.viewpager.widget.ViewPager
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import com.alibaba.android.arouter.facade.annotation.Route
import com.tao.lab.ui.main.SectionsPagerAdapter
import com.tao.lab.databinding.ActivityMainBinding
import kotlinx.coroutines.*

@Route(path = "app/MainActivity")
class MainActivity : AppCompatActivity() {
    private val TAG = "MainActivity"
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
        val viewPager: ViewPager = binding.viewPager
        viewPager.adapter = sectionsPagerAdapter
        val tabs: TabLayout = binding.tabs
        tabs.setupWithViewPager(viewPager)
        val fab: FloatingActionButton = binding.fab

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
        addition_isCorrect();
    }

    fun addition_isCorrect() {
        var arrs = arrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
        GlobalScope.launch(Dispatchers.Main) {
            arrs.forEach {
                Log.i(TAG, "进入循环 $it")
                var index = printMsg(it);
                Log.i(TAG, "跳出循环：$index   线程： ${Thread.currentThread().name}")
            }

        }

    }

    suspend fun printMsg(index: Int) = withContext(Dispatchers.IO) {
        delay(100)
        Log.i(TAG, "线程： ${Thread.currentThread().name}")
        index
    }

    override fun onResume() {
        super.onResume()

    }
}